using System;
using System.Collections;
using System.Data;
using System.Web.UI.WebControls;
using Model;
using Service;
using Parameter=Model.Parameter;

public partial class Login : System.Web.UI.Page
{
    readonly ReferenceDataService _ReferenceDataService = new ReferenceDataService();
    private readonly DirectServiceLogin _DriveServiceLogin = new DirectServiceLogin();
    protected void Page_Load(object sender, EventArgs e)
    {
        

    }

    protected void m_login_Authenticate(object sender, AuthenticateEventArgs e)
    {
        try
        {
            IList listEmployee = new ArrayList();
            listEmployee = _ReferenceDataService.GetEmployees();
            for (int i = 0; i < listEmployee.Count; i++)
            {
                string userLogin = ((Employee) (listEmployee[i])).UserName;
                string password = ((Employee) (listEmployee[i])).Password;

                if (m_login.UserName == userLogin && m_login.Password == password)
                {
                    int groupId = ((Employee) (listEmployee[i])).GroupId;
                    DataTable table = new DataTable();
                    table = _DriveServiceLogin.GetPermission(groupId);
                   // IList lstPermission = new ArrayList();
                    Hashtable hashtablePermission = new Hashtable();
                    if (table.Rows.Count > 0)
                    {
                        for (int j = 0; j < table.Rows.Count; j++)
                        {
                           
                            string permissionId = table.Rows[j]["PermissionId"].ToString();
                            hashtablePermission.Add(permissionId, permissionId);
  
                        }
                      //  Parameter.permission = lstPermission;
                        Parameter.hashTablePermission = hashtablePermission;
                    }
                    Response.Redirect("Default.aspx");
                    return;
                }
                
            }

            

         //  
            
            //bool result=DoLogin(m_login.UserName, m_login.Password);
            
            //e.Authenticated = result;            
        }
        catch (Exception ex)
        {
            this.Validators.Add(new ValidatorMessage(ex.ToString()));
        }
    }
    private bool DoLogin(string username, string password)
    {
        /*
        bool result=Membership.ValidateUser(username, password);
       
        return result;
        */
       
        if (username == "admin")
        {
            Session["user"] = username;
            return true;
        }
        else
        {
            return false;
        }
    }
}
